import os
import json
import time
import random
from selenium import webdriver
from bs4 import BeautifulSoup as bf
from tqdm import tqdm
from selenium.webdriver import ChromeOptions
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from PIL import Image
import base64
store_root = r"E:\1"

def is_image_full(filename):
    img = Image.open(filename)
    img_size = img.size #图片的长和宽
    min_size = min(img_size) #图片的短边
    print("minsize:{}".format(min_size))
    if min_size>1000:
        return True
    else:
        return False

def base64_to_img(bstr, file_path):
    imgdata = base64.b64decode(bstr)
    file = open(file_path, 'wb')
    file.write(imgdata)
    file.close()

def get_browser(url):
    browser = webdriver.Chrome()
    browser.get(url)
    return browser

def keys_enter_click(browser):
    htmlElem = browser.find_element_by_tag_name('html')
    htmlElem.send_keys(Keys.ENTER)

def refresh_page(browser):
    browser.refresh()

def is_source_loaded(browser,idx):
    a = get_page_by_idx(browser,idx)
    img_div = a.select("img")[0]
    try:
        if img_div["src"][:4]:
            return True
        else:
            return False
    except:
        return False

def get_page_by_idx(browser,idx):
    html = browser.page_source
    doc_model = bf(html,'html.parser')
    page = doc_model.select("#pagebox > div[index='{}']".format(idx))[0]
    return page

def turn_to_page(browser,idx):
    print("turn to page {}".format(idx))
    button_pages = browser.find_element(By.CSS_SELECTOR,"#app > div.page > div.page-head > div > div > div.page-head-page > div.page-head-tol")
    button_input = browser.find_element(By.CSS_SELECTOR,"#app > div.page > div.page-head > div > div > div.page-head-page > div.el-input > input")
    button_pages.click()
    button_input.send_keys(str(idx))
    keys_enter_click(browser)

def get_book_pages(browser):
    html = browser.page_source
    doc_model = bf(html,'html.parser')
    pages = doc_model.select("#pagebox > div")
    book_pages = len(pages)-1
    return book_pages

def is_store_img(browser,idx,store_path):
    a = get_page_by_idx(browser,idx)
    img_div = a.select("img")[0]
    img_base64 = img_div["src"].split(",")[1]
    file_path = os.path.join(store_path,"{}.jpg".format(idx))
    base64_to_img(img_base64,file_path)
    print("image:{}.jpg downloaded".format(idx))
    if is_image_full(file_path):
        return True
    else:
        return False

def get_store_root (url):
    book_id = url.split("=")[1]
    store_path = os.path.join(store_root,book_id)
    try:
        os.mkdir(store_path)
        return store_path
    except Exception as e:
        print(e)
        return store_path

def crawl_book(url,idx):
    store_path =get_store_root(url)
    browser = get_browser(url)
    pages = get_book_pages(browser)
    #idx = 235
    while(idx <= pages):
        turn_to_page(browser,idx)
        time.sleep(1)
        if is_source_loaded(browser,idx):
            if is_store_img(browser,idx,store_path):
                time.sleep(1)
                idx = idx+1
                pass
            else:
                print("image not complete , refreshing page>>>")
                refresh_page(browser)
                time.sleep(3)
        else:
            print("source is not loaded , refreshing page>>>")
            refresh_page(browser)
            time.sleep(3)

    print("finished")

# 乌合之众 https://lib-hfut.wqxuetang.com/read/pdf?bid=3187095
# 精神分析导论 https://lib-hfut.wqxuetang.com/read/pdf?bid=3184035
# 日常生活心理病理学 https://lib-hfut.wqxuetang.com/read/pdf?bid=10482
# 自我与本我 https://lib-hfut.wqxuetang.com/read/pdf?bid=3204104
# 信号处理与分析  https://lib-hfut.wqxuetang.com/read/pdf?bid=193398
# 运筹学 https://lib-hfut.wqxuetang.com/read/pdf?bid=3202291
# 超越自卑 https://lib-hfut.wqxuetang.com/book/3184027
# 荣格自传 https://lib-hfut.wqxuetang.com/read/pdf?bid=3187036
# 全民心理学 https://lib-hfut.wqxuetang.com/read/pdf?bid=1400
# 矩阵分析基础 https://lib-hfut.wqxuetang.com/read/pdf?bid=3183567
# 高等数学 https://lib-hfut.wqxuetang.com/read/pdf?bid=4681
# 概率论和数理统计 https://lib-hfut.wqxuetang.com/read/pdf?bid=3207925
# 推荐系统实践 https://lib-hfut.wqxuetang.com/read/pdf?bid=223134
# 深度学习与推荐系统 https://lib-hfut.wqxuetang.com/read/pdf?bid=3207561

if __name__ == '__main__':
    a = "https://lib-hfut.wqxuetang.com/read/pdf?bid=3207561"
    idx = 1
    crawl_book(a,idx)
